'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    // 创建销项发票表
    await queryInterface.createTable('outgoing_invoices', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      invoice_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '发票号码'
      },
      invoice_code: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '发票代码'
      },
      invoice_type_code: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: '发票类型代码'
      },
      invoice_kind_code: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: '发票种类代码'
      },
      invoice_status_code: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: '发票状态代码'
      },
      is_red_invoice: {
        type: Sequelize.STRING(1),
        allowNull: true,
        comment: '是否红字发票(Y/N)'
      },
      seller_tax_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '销方识别号'
      },
      seller_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '销方名称'
      },
      buyer_tax_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '购方识别号'
      },
      buyer_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '购方名称'
      },
      invoice_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '开票日期'
      },
      total_amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: true,
        comment: '金额'
      },
      tax_amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: true,
        comment: '税额'
      },
      total_tax_amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: true,
        comment: '价税合计'
      },
      invoice_source: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '发票来源'
      },
      issuer: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '开票人'
      },
      tax_bureau_code: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '税务机关代码'
      },
      tax_bureau_name: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '税务机关名称'
      },
      organization_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '组织ID'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // 创建索引
    await queryInterface.addIndex('outgoing_invoices', ['invoice_number'], {
      name: 'idx_invoice_number'
    });
    
    await queryInterface.addIndex('outgoing_invoices', ['seller_tax_number'], {
      name: 'idx_seller_tax_number'
    });
    
    await queryInterface.addIndex('outgoing_invoices', ['buyer_tax_number'], {
      name: 'idx_buyer_tax_number'
    });
    
    await queryInterface.addIndex('outgoing_invoices', ['invoice_date'], {
      name: 'idx_invoice_date'
    });
  },

  async down (queryInterface, Sequelize) {
    await queryInterface.dropTable('outgoing_invoices');
  }
};